﻿using System;
using Cysharp.Threading.Tasks;
using FrameWork.Util;
using UnityEngine;
using YooAsset;
using Object = UnityEngine.Object;

namespace FrameWork.Managers
{
    public class ResourcesManager : BaseManager
    {
        public static ResourcesManager GetInstance()
        {
            return GameManager.Instance.ResourcesManager;
        }

        #region 生命周期
        internal ResourcesManager(ConstructorToken constructorToken = null) : base(constructorToken)
        {
        }

        public override async UniTask OnInit()
        {
            await InitYooAsset();
        }
        
        #endregion

        private ResourcePackage DefaultPackage { get; set; }

        private async UniTask InitYooAsset()
        {
           
            await InitPackage();
        }

        private async UniTask InitPackage()
        {  
            YooAssets.Initialize();
            
            //以下还是编辑器环境的模拟流程不包含打包以后的真实初始化流程后面 应该加上 todo
            var package = YooAssets.CreatePackage("DefaultPackage");
            YooAssets.SetDefaultPackage(package);
            
            var buildResult = EditorSimulateModeHelper.SimulateBuild("DefaultPackage");    
            var packageRoot = buildResult.PackageRootDirectory;
            var editorFileSystemParams = FileSystemParameters.CreateDefaultEditorFileSystemParameters(packageRoot);
            var initParameters = new EditorSimulateModeParameters
            {
                EditorFileSystemParameters = editorFileSystemParams
            };
            
            var initOperation = package.InitializeAsync(initParameters);
            await initOperation;
            
            DefaultPackage = package;
            if(initOperation.Status == EOperationStatus.Succeed)
                FLogger.Log("资源包初始化成功！");
            else 
                FLogger.LogError($"资源包初始化失败：{initOperation.Error}");

            // 2. 请求资源清单的版本信息
            var requestPackageVersionOperation = package.RequestPackageVersionAsync();
            await requestPackageVersionOperation;
            // Debug.LogError($"--- requestPackageVersionOperation = {requestPackageVersionOperation.PackageVersion}");
            
            // 3. 传入的版本信息更新资源清单
            await package.UpdatePackageManifestAsync(requestPackageVersionOperation.PackageVersion);
        }

        #region 加载资源

        public async void LoadSprite(string path, Action<UnityEngine.UI.Image> callBack)
        { 
            try
            {
                await UniTask.CompletedTask;
            }
            catch (Exception e)
            {
                FLogger.LogError($"------------- e : {e.Message}");
            }
        }

        public async UniTask<GameObject> LoadPrefab(string path)
        {
            if (string.IsNullOrEmpty(path) || string.IsNullOrWhiteSpace(path))
            {
                return null;
            }
            
            // var assetHandle = YooAssets.LoadAssetAsync<GameObject>(path);
            var assetHandle = DefaultPackage.LoadAssetAsync<GameObject>(path);
            await assetHandle;
            var result = assetHandle.AssetObject as GameObject;
            return Object.Instantiate(result);
        }
        
        #endregion
    }
    
    public static class ResourcesExt
    {
        public static void SetIcon(this UnityEngine.UI.Image image, string path)
        {
            // todo yooAsset 加载 
            // var sprite = YooAsset.
            // image.sprite = sprite
        }
    }
}